package com.dw.widget.slideshow.config;

import java.util.List;

import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.res.Resources;
import android.os.Bundle;

import com.dw.widget.slideshow.config.view.list.BaseListItemView;
import com.dw.widget.slideshow.config.view.list.ListBigIconSelectView;
import com.dw.widget.slideshow.config.view.list.ListBigIconSelectView.OnCheckBoxSwitchListener;
import com.dw.widget.slideshow.utils.AppCommon;
import com.dw.widget.slideshow.utils.AppConfig;
import com.dw.widget.slideshow.utils.ApplicationEx;
import com.dw.widget.slideshow.utils.CommonUtils;
import com.dw.widget.slideshow.R;
import com.dw.widget.slideshow.R.drawable;

public class AnimationChooserActivity extends BaseBigIconSelectActivity {

	private final ApplicationEx _APP = ApplicationEx.INSTANCE();
	
	public void initListData()
	{
		Resources res = this.getResources();
		listItemData = new Object[][]{{R.drawable.icon_animation01, res.getString(R.string.animation_slideup), false},
									  {R.drawable.icon_animation02, res.getString(R.string.animation_fade), false},
									  {R.drawable.icon_animation03, res.getString(R.string.animation_zoom), false},
									  {R.drawable.icon_animation04, res.getString(R.string.animation_fan), false},
									  {R.drawable.icon_animation05, res.getString(R.string.animation_fan2), false},
									  {R.drawable.icon_animation06, res.getString(R.string.animation_metro), false}};

	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		initListData();
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void initViews()
	{
		super.initViews();
		header.setTitle(getResources().getString(R.string.choose_animation_tip));
		
		final List<BaseListItemView> listItems = listBlock.getListItems();
		
		// 读取配置项
		final AppConfig appConfig = new AppConfig(AnimationChooserActivity.this, _APP.CONFIG_FILE_NAME);
		int selectedAnimation = appConfig.getInt(_APP.CONFIG_ITEM_ANIMATION, _APP.ANIMATION_STYLE1);
		int selectedIdx = selectedAnimation - _APP.ANIMATION_STYLE1;
		ListBigIconSelectView biv = (ListBigIconSelectView)listItems.get(selectedIdx);
		biv.getCheckBox().setChecked(true);
		
		final int count = listItems.size();
		for(int i = 0; i < count; i++)
		{
			final int index = i;
			BaseListItemView item = listItems.get(i);
			ListBigIconSelectView bigIconView = (ListBigIconSelectView)item;
			bigIconView.setOncheckBoxSwitchListener(new OnCheckBoxSwitchListener() {
				
				@Override
				public void OnSwitch() {
					
					if(_APP.isProfessional == false && index != 0)
					{
						CommonUtils.showDialog(AnimationChooserActivity.this, AnimationChooserActivity.this.getString(R.string.become_professional),
								AnimationChooserActivity.this.getString(R.string.professional_tip), new OnClickListener() {
									
									@Override
									public void onClick(DialogInterface dialog, int which) {
										
										AppCommon.installProfessionalEdition(AnimationChooserActivity.this);
									}
								});
					}
					else if(index == count - 1)    // 最后一个
					{
						CommonUtils.showShortToast(AnimationChooserActivity.this, AnimationChooserActivity.this.getString(R.string.coming_soon));
					}
					else
					{
						// 把其他设置项设为未选中
						for(int j = 0; j < count; j++)
						{
							ListBigIconSelectView biv = (ListBigIconSelectView)listItems.get(j);
							
							if(j == index)
							{
								biv.getCheckBox().setChecked(true);
								continue;
							}
							
							biv.getCheckBox().setChecked(false);
						}
						
						// 更新配置文件
						int selectAnimation = _APP.ANIMATION_STYLE1 + index;
						
						final AppConfig appConfig = new AppConfig(AnimationChooserActivity.this, _APP.CONFIG_FILE_NAME);
						appConfig.putInt(_APP.CONFIG_ITEM_ANIMATION, selectAnimation);
						
						// 关闭窗体
						AnimationChooserActivity.this.finish();
					}
				}
			});
		}
	}
}
